package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.DeliveryExtendMapper;
import com.aiwei.tdjk.dto.request.DeliveryPagerRequest;
import com.aiwei.tdjk.dto.response.HasDeliveryItemResponse;
import com.aiwei.tdjk.entity.*;
import com.aiwei.tdjk.enums.DeliveryStatusEnum;
import com.aiwei.tdjk.service.DeliveryDetailService;
import com.aiwei.tdjk.service.DeliveryService;
import com.aiwei.tdjk.service.SaleOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DeliveryServiceImpl implements DeliveryService{

    @Autowired
    private DeliveryExtendMapper deliveryExtendMapper;

    @Autowired
    private DeliveryDetailService deliveryDetailService;

    @Autowired
    private SaleOrderItemService saleOrderItemService;



    /**
     * 分页查询
     * @param request
     * @return
     */
    @Override
    public Pager<Delivery> find(DeliveryPagerRequest request) {
        request.webParamConvert();
        List<Delivery> list = deliveryExtendMapper.find(request);
        int totalCount = deliveryExtendMapper.getTotalCount(request);
        Pager<Delivery> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(list);
        pager.setTotalCount(totalCount);
        return pager;
    }


    /**
     * 获取
     * @param id
     * @return
     */
    @Override
    public Delivery get(Long id){
        return deliveryExtendMapper.selectByPrimaryKey(id);
    }


    /**
     * 新增
     *
     * @param delivery
     * @return
     */
    @Override
    public int add(Delivery delivery) {
        return deliveryExtendMapper.insertSelective(delivery);
    }

    /**
     * 更新
     *
     * @param delivery
     */
    @Override
    public void update(Delivery delivery) {
        deliveryExtendMapper.updateByPrimaryKeySelective(delivery);
    }

    @Override
    public void changeStatus(Long[] ids, DeliveryStatusEnum newStatus) {
        for (Long id : ids) {
            Delivery delivery = get(id);
            //判断手册的状态是否为：1、2；
            if (delivery.getStatus()==DeliveryStatusEnum.unlock.getCode()||delivery.getStatus()==DeliveryStatusEnum.lock.getCode()){
                delivery.setStatus(newStatus.getCode());
                update(delivery);
            }

        }
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        deliveryExtendMapper.deleteByPrimaryKey(id);
    }




    /**
     * 查询某个订单的已经发货的订单项目和发货数量
     *
     * @param saleOrderCode 订单号码
     *
     * @return 订单项目号=》已经发送的数量
     */
    @Override
    public HashMap<String,Integer> getHasDeliveryItemList(String saleOrderCode) {

        //根据订单号码查询发货单的外键id的列表
        DeliveryExample example = new DeliveryExample();
        example.or().andSaleOrderCodeEqualTo(saleOrderCode);
        List<Delivery> deliveryList = deliveryExtendMapper.selectByExample(example);
        //根据发货单的id查询已经发货的列表的列表

        HashMap<String,Integer> hashMap = new HashMap<>();

        List<HasDeliveryItemResponse> list = new ArrayList<>();

        List<SaleOrderItem> saleOrderItems = saleOrderItemService.getListByOrderCode(saleOrderCode);

        for (SaleOrderItem saleOrderItem:saleOrderItems){
            HasDeliveryItemResponse response = new HasDeliveryItemResponse();
            response.setSaleOrderItemCode(saleOrderItem.getSaleorderitemcode());
            list.add(response);

            hashMap.put(saleOrderItem.getSaleorderitemcode(),0);
        }

        for (Delivery delivery:deliveryList){
            List<DeliveryDetail> deliveryDetailList = deliveryDetailService.getListByDeliveryId(delivery.getId());

            for (DeliveryDetail deliveryDetail:deliveryDetailList){
                Integer integer = hashMap.get(deliveryDetail.getSaleOrderItemCode());
                hashMap.put(deliveryDetail.getSaleOrderItemCode(),integer+deliveryDetail.getDeliveryQty());
            }

        }

        return hashMap;
    }

    @Override
    public HashMap<String, Integer> getHasNotDeliveryItemList(String saleOrderCode) {

        List<SaleOrderItem> saleOrderItems = saleOrderItemService.getListByOrderCode(saleOrderCode);

        HashMap<String, Integer> hasDeliveryItemMap = getHasDeliveryItemList(saleOrderCode);

        HashMap<String,Integer> hashMap = new HashMap<>();

        for (SaleOrderItem saleOrderItem:saleOrderItems){
//            hashMap.put(saleOrderItem.getSaleorderitemcode(),saleOrderItem.getPlanquty());
            //todo 暂时使用计划数量
            hashMap.put(saleOrderItem.getSaleorderitemcode(),saleOrderItem.getPlanquty()-hasDeliveryItemMap.get(saleOrderItem.getSaleorderitemcode()));
        }

        return hashMap;
    }


    /**
     * 根据订单号查询，已经制定的发货单号
     */
    @Override
    public List<Delivery> getListBySaleOrderCode(String saleOrderCode) {
        DeliveryExample example= new DeliveryExample();
        example.or().andSaleOrderCodeEqualTo(saleOrderCode);
        List<Delivery> deliveryList = deliveryExtendMapper.selectByExample(example);
        return deliveryList;
    }

    /**
     * @param bookingContainerId
     */
    @Override
    public void deleteDeliveryByBookingContainerId(Long bookingContainerId) {
        DeliveryExample example = new DeliveryExample();
        example.or().andBookingContainerIdEqualTo(bookingContainerId);
//        Delivery delivery = new Delivery();
//        delivery.setStatus(DeliveryStatusEnum.deleted.getCode());
//        deliveryExtendMapper.updateByExample(delivery,example);
        deliveryExtendMapper.deleteByExample(example);
    }

}
